package 因子分解和枚举.n的第k个因子;

import java.util.ArrayList;

/**
 * TODO
 *
 * @className: nddkgyz
 * @description: TODO
 * @author: 等什么柠檬君
 * @date: 2022-05-07 00:18
 **/
public class nddkgyz {
    public static void main(String[] args) {
        System.out.println(kthFactor(12, 3));
        System.out.println(kthFactor(7, 2));
        System.out.println(kthFactor(4, 4));
    }
    public static int kthFactor(int n, int k) {
        ArrayList<Integer> arrayList = new ArrayList();
        for (int i = n; i >= 1; --i) {
            if (n % i == 0){
                arrayList.add(n / i);
            }
        }
        return arrayList.size()<k?-1:arrayList.get(k-1);
    }
}
